package com.client.action;

import com.client.bean.FeedBack;
import com.client.bean.Report;
import com.client.service.SoftwareDetailClientService;
import com.data.util.CheckDataUtils;
import com.data.util.ImageLookUtils;
import com.data.util.JsonUtils;
import com.data.util.SoftwareMassageUtils;
import com.user.bean.Speaking;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 软件详情，即App端的二级界面相关的处理请求
 *
 * @author:HingLo
 * @create 2017-10-10 15:51
 **/
@Controller
@RequestMapping("/softwareDetailClient/client/")
public class SoftwareDetailClientAction {
    @Autowired
    private SoftwareDetailClientService softwareDetailClientService;

    /**
     * 进入到软件详情界面,根据软件的id查找出所有的展示图
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("findSoftwareDetail")
    @ResponseBody
    public Map<String, Object> findSoftwareDetailAction(String id, HttpServletRequest request) {
        List<?> list = softwareDetailClientService.findSoftwareDetailService(id);
        String bigImageUrl = JsonUtils.getBasePath(request) + "/softwareDetailClient/client/lookBigImage?rid=";
        Map<String, Object> map = JsonUtils.detailSoftwareDetailUtils(list, bigImageUrl);
        return map;
    }

    /**
     * 根据与软件的id与大图的名称来查看软件的大图
     *
     * @param rid
     * @param name
     * @param request
     * @param response
     */
    @GetMapping("lookBigImage")
    public void lookBigImageAction(String rid, String name, HttpServletRequest request, HttpServletResponse response) {

        if (CheckDataUtils.stringUtils(rid, name)) {
            byte[] inputStream = softwareDetailClientService.lookBigImageService(rid, name);
            ImageLookUtils.responseImage(inputStream, response);
        }

    }

    /**
     * 查看软件的评论信息
     *
     * @param rid 软件的id
     * @return
     */
    @RequestMapping("lookSpeakInfo")
    @ResponseBody
    public Map<String, Object> lookSpeakInfoAction(String rid, int start, int end, HttpServletRequest request) {
        List<?> list = softwareDetailClientService.lookSpeakInfoService(rid, start, end);
        String[] value = {"id", "name", "contents", "ctime", "start", "good"};
        return SoftwareMassageUtils.getSoftwareMassageUtils(list, request, null, value);
    }

    /**
     * 给评论点赞
     *
     * @param id 评论的id
     * @return
     */
    @RequestMapping("addSpeakingGood")
    @ResponseBody
    public Map<String, Object> addSpeakingGoodAction(String id) {
        boolean bool;
        bool = softwareDetailClientService.addSpeakingGoodService(id);
        return JsonUtils.returnMassageUtils(true, JsonUtils.returnMassageUtils(bool, bool ? "点赞成功" : "点赞失败"));


    }

    /**
     * 提交评论
     *
     * @param speaking 评论的对象
     * @param request
     * @return
     */
    @PostMapping("addSpeakingSoftware")
    @ResponseBody
    public Map<String, Object> addSpeakingSoftwareAciton(Speaking speaking, HttpServletRequest request) {
        boolean bool;
        speaking.setIp(request.getRemoteAddr());
        bool = softwareDetailClientService.addSpeakingSoftwareService(speaking);
        return JsonUtils.returnMassageUtils(true, JsonUtils.returnMassageUtils(bool, bool ? "评论成功" : "评论失败"));


    }

    /**
     * 反馈软件信息
     *
     * @param feedBack
     * @return
     */

    @PostMapping("feedBackSoftware")
    @ResponseBody
    public Map<String, Object> feedBackSoftwareAction(FeedBack feedBack) {
        boolean bool = softwareDetailClientService.feedBackSoftwareService(feedBack);
        return JsonUtils.returnMassageUtils(true, JsonUtils.returnMassageUtils(bool, bool ? "反馈成功" : "反馈失败"));

    }

    /**
     * 举报软件
     *
     * @return
     */
    @PostMapping("reportSoftware")
    @ResponseBody
    public Map<String, Object> reportSoftwareAction(Report report) {
        boolean bool = softwareDetailClientService.reportSoftwareService(report);
        return JsonUtils.returnMassageUtils(true, JsonUtils.returnMassageUtils(bool, bool ? "举报成功" : "举报失败"));

    }

    /**
     * 收藏软件
     *
     * @param rid
     * @param uid
     * @return
     */
    @RequestMapping("collectionSoftware")
    @ResponseBody
    public Map<String, Object> collectionSoftwareAction(String rid, String uid, int type) {
        boolean bool;
        if (CheckDataUtils.stringUtilsID(uid) && CheckDataUtils.stringUtils(rid)) {
            //收藏软件
            if (type == 0) {
                bool = softwareDetailClientService.CollectionSoftwareService(uid, rid);
                return JsonUtils.returnMassageUtils(true, JsonUtils.returnMassageUtils(bool, bool ? "收藏成功" : "收藏失败"));
                //取消收藏软件
            } else if (type == 1) {
                return deleteCollectionSoftwareAction(rid, uid);
            }

        }
        return JsonUtils.returnMassageUtils(true, JsonUtils.returnMassageUtils(false, "操作失败"));

    }

    /**
     * 删除收藏软件
     *
     * @param rid
     * @param uid
     * @return
     */
    @RequestMapping("deleteCollectionSoftware")
    @ResponseBody
    public Map<String, Object> deleteCollectionSoftwareAction(String rid, String uid) {
        boolean bool = softwareDetailClientService.deleteCollectionSoftwareService(uid, rid);
        return JsonUtils.returnMassageUtils(true, JsonUtils.returnMassageUtils(bool, bool ? "取消收藏成功" : "取消收藏失败"));
    }


    /**
     * 查询收藏软件列表
     *
     * @param uid
     * @return
     */
    @RequestMapping("findCollectionSoftwarelist")
    @ResponseBody
    public Map<String, Object> findCollectionSoftwareAction(String uid, HttpServletRequest request) {
        List<?> list = softwareDetailClientService.findCollectionSoftwareService(uid);
        return SoftwareMassageUtils.getSoftwareMassageUtils(list, request);

    }

    /**
     * 查询该软件是否收藏软件 如果返回true，表示已经收藏，返回false表示未收藏
     *
     * @param uid
     * @param rid
     * @return
     */
    @RequestMapping("findcollectionSoftware")
    @ResponseBody
    public Map<String, Object> findCollectionSoftwareAction(String uid, String rid) {

        boolean bool = softwareDetailClientService.findCollectionSoftwareService(uid, rid);
        return JsonUtils.returnMassageUtils(true, JsonUtils.returnMassageUtils(bool, bool ? "已经收藏" : "未收藏"));

    }


    /**
     * 根据关键字搜索软件
     *
     * @param request
     * @param search
     * @return
     */
    @RequestMapping("search")
    @ResponseBody
    public Map<String, Object> searchSoftwareAction(HttpServletRequest request, String search) {
        //名字搜索
        List<?> list = softwareDetailClientService.searchSoftwareService(search);
        //标签搜索
        List<?> listTag = softwareDetailClientService.searchTagSoftwareService(search);
        List<Object> list1 = new ArrayList<>();
        list1.addAll(list);
        list1.addAll(listTag);
        return SoftwareMassageUtils.getSoftwareMassageUtils(list1, request);

    }


    /**
     * 搜索Tag
     *
     * @param request
     * @return
     */
    @RequestMapping("searchHot")
    @ResponseBody
    public Map<String, Object> searchTagSoftwareAction(HttpServletRequest request) {
        //默认热搜条数
        int len = 10;
        List<?> list = softwareDetailClientService.searchTagsSoftwareService(len);
        return JsonUtils.returnMassageUtils(true, list);

    }

    /**
     * 详情页的推荐软件列表
     *
     * @param request
     * @return
     */
    @RequestMapping("recommend")
    @ResponseBody
    public Map<String, Object> recommendSoftwareAction(String rid, HttpServletRequest request) {
        //默认热搜条数
        int len = 5;
        List<?> list = softwareDetailClientService.recommendSoftwareService(rid, len);
        return SoftwareMassageUtils.getSoftwareMassageUtils(list, request);

    }

    /**
     * 更新管理器
     *
     * @param request
     * @return
     */
    @RequestMapping("updateAdmin")
    @ResponseBody
    public Map<String, Object> updateAdminSoftwareAction(String packageName, HttpServletRequest request) {
        List<?> list = softwareDetailClientService.updateAdminSoftwareService(packageName, true);
        return SoftwareMassageUtils.getSoftwareMassageUtils(list, request);

    }

    /**
     * 更新管理器
     *
     * @param request
     * @return
     */
    @RequestMapping("updateAdmins")
    @ResponseBody
    public Map<String, Object> updateAdminsSoftwareAction(String rid, HttpServletRequest request) {
        List<?> list = softwareDetailClientService.updateAdminSoftwareService(rid, false);
        return SoftwareMassageUtils.getSoftwareMassageUtils(list, request);
    }


    /**
     * @param rid
     * @return
     */
    @RequestMapping("speaklook")
    @ResponseBody
    public Map<String, Object> speakLookAction(String rid) {
        List<?> list = softwareDetailClientService.speakLookService(rid);
        String value[] = {"a", "b", "c", "d", "e"};
        return JsonUtils.returnMassageUtils(!list.isEmpty(), JsonUtils.returnMassageUtils(value, list));

    }

}
